#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2004 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
####################################################################
#
#   lsswstatstart command
#
#   Syntax: lsswstatstart (-f <frame> -c <cage> | -a) [ -s <start> 
#               -o <stop> -i <interval> -n <filename> -z <filesize> 
#               -l <lane> -t <type(send/receive)> 
#               -r <reset counters, type(default: send) and lane(default: 5)> -d <directory> | --help ]
#
#            --help               - Prints this message
#
####################################################################
#
#   Initial version - 02/04/04
#
####################################################################

sub exit_routine {
    
     # remove files from /tmp 
     
     exit $ret; 

} # end exit_routine subroutine

sub validate_props {

	if ($frame_parm =~ /\D+/) {
		# non-digit in frame field
		print "non-digit in frame field\n\n";
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}
		
	if ($cage_parm =~ /\D+/) {
		# non-digit in cage field
		print "non-digit in cage field\n\n";
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}

	if ($lane_parm =~ /\D+/) {
		# non-digit in $lane_parm field
		print "non-digit in lane field\n\n";
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}
		
	if ($start_parm =~ /\D+/) {
		# non-digit in $start_parm field
		print "non-digit in start time field\n\n";
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}
		
	if ($stop_parm =~ /\D+/) {
		# non-digit in $stop_parm field
		print "non-digit in stop time field\n\n";
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}

	if ($interval_parm =~ /\D+/) {
		# non-digit in $interval_parm field
		print "non-digit in interval field\n\n";
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}
		
	if ($filesize_parm =~ /\D+/) {
		# non-digit in $filesize_parm field
		print "non-digit in file size field\n\n";
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}
}

################################################################################
#
# start main body of code
#
################################################################################
$PRGRM  = "lsswstatstart";
$BIN    = "/opt/hsc/bin"; 
$PARMS = 0;
$f_USED = 0;
$c_USED = 0;
$l_USED = 0;
$t_USED = 0;
$a_USED = 0;
$s_USED = 0;
$o_USED = 0;
$i_USED = 0;
$n_USED = 0;
$z_USED = 0;
$r_USED = 0;
$d_USED = 0;
#default params
$frame_parm = 0;
$cage_parm = 0;
$lane_parm = 5;
$type_parm = "send";
$start_parm = 0;
$stop_parm = 0;
$interval_parm = 60;
$filename_parm = switchStats;
$filesize_parm = 10;
$directory_parm = "/tmp/switchStats/";
@validflags = ("-f", "-c", "-l", "-t", "-a", "-s", "-o", "-i", "-n", "-z", "-r", "-d");
		
$USAGE = "Usage: lsswstatstart (-f <frame> -c <cage> | -a) [ -s <start> 
                 -o <stop> -i <interval> -n <filename> -z <filesize(MB)> 
                 -l <lane> -t <type(send/receive)> 
                 -r <reset counters, type(default: send) and lane(default: 5)> -d <directory> | --help ]\n";

# First check for existence of parameters
if ($#ARGV ne -1) {
	$PARMS = 1;
}

$i=0;
# Determine if -f was used
foreach $ARGV (@ARGV) {
	if ($ARGV =~ /^\s*-f\S*$/ ) { 
		$f_USED = 1;
        $frame_parm = $ARGV[$i + 1];		
	}
	$i++;
	
}

$i=0;
# Determine if -c was used
foreach $ARGV (@ARGV) {
	if ($ARGV =~ /^\s*-c\S*$/ ) { 
		$c_USED = 1;
        $cage_parm = $ARGV[$i + 1];		
	}
	$i++;
}

# Determine if -a was used
foreach $ARGV (@ARGV) {
	if ($ARGV =~ /^\s*-a\S*$/ ) { 
		$a_USED = 1;
	}
}

$i=0;
# Determine if -l was used
foreach $ARGV (@ARGV) {
	if ($ARGV =~ /^\s*-l\S*$/ ) { 
		$l_USED = 1;
        $lane_parm = $ARGV[$i + 1];		
	}
	$i++;
}

$i=0;
# Determine if -t was used
foreach $ARGV (@ARGV) {
	if ($ARGV =~ /^\s*-t\S*$/ ) { 
		$t_USED = 1;
        $type_parm = $ARGV[$i + 1];		
	}
	$i++;
}

$i=0;
# Determine if -s was used
foreach $ARGV (@ARGV) {
	if ($ARGV =~ /^\s*-s\S*$/ ) { 
		$s_USED = 1;
        $start_parm = $ARGV[$i + 1];		
	}
	$i++;
}

$i=0;
# Determine if -o was used
foreach $ARGV (@ARGV) {
	if ($ARGV =~ /^\s*-o\S*$/ ) { 
		$o_USED = 1;
        $stop_parm = $ARGV[$i + 1];		
	}
	$i++;
}

$i=0;
# Determine if -i was used
foreach $ARGV (@ARGV) {
	if ($ARGV =~ /^\s*-i\S*$/ ) { 
		$i_USED = 1;
        $interval_parm = $ARGV[$i + 1];		
	}
	$i++;
}

$i=0;
# Determine if -n was used
foreach $ARGV (@ARGV) {
	if ($ARGV =~ /^\s*-n\S*$/ ) { 
		$n_USED = 1;
        $filename_parm = $ARGV[$i + 1];		
	}
	$i++;
}

$i=0;
# Determine if -z was used
foreach $ARGV (@ARGV) {
	if ($ARGV =~ /^\s*-z\S*$/ ) { 
		$z_USED = 1;
        $filesize_parm = $ARGV[$i + 1];		
	}
	$i++;
}

# Determine if -r was used
foreach $ARGV (@ARGV) {
	if ($ARGV =~ /^\s*-r\S*$/ ) { 
		$r_USED = 1;
	}
}

$i=0;
# Determine if -d was used
foreach $ARGV (@ARGV) {
	if ($ARGV =~ /^\s*-d\S*$/ ) { 
		$d_USED = 1;
        $directory_parm = $ARGV[$i + 1];		
	}
	$i++;
}

# Determine if there are any invalid flags
$cr=0;
foreach $ARGV (@ARGV) {
        $valid = 0;
	if ($ARGV =~ /^\s*-\D\S*$/ ) { 
            foreach $validflags (@validflags) {
	        if ($validflags eq $ARGV ) { 
                    $valid=1;
	        }
            } 
	    if ($valid == 0) { 
                print $ARGV;
                print " is not a valid flag.\n";
                $cr=1;
	   } 
	}
}

if ($cr==1) { 
      print "\n";
}

# If there are parms, need to validate and parse
if ($PARMS == 1) {
	# Check for --help (usage)
	foreach $ARGV (@ARGV) {
		if ($ARGV =~ /^\s*--help\S*$/ ) { 
			# Help was specified
			print $USAGE;    #USAGE
			if ($#ARGV == 0) {
				$ret = 0;
			}
			else {
				# If help and anything else was specified,
				#      it's an error
				$ret = 1;
			}
			&exit_routine;
		} #end if
	} #end foreach

        if (($t_USED == 1)) {
            # 
            if ($type_parm eq "send")
            {
                $type_parm=1;
            }
            elsif ($type_parm eq "receive")
            {
                $type_parm=0;
            }
            else {
		print "Type (-t) must be either \"send\" or \"receive\"\n\n";
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
		    }
	}
	
        if (($i_USED == 1) && (interval_parm < 60)) {	
    		print "The interval is set to less than sixty seconds.\n";
            print "It is recommended to set the interval at sixty seconds or greater to avoid potentially monopolizing the serial links.\n\n";     #DEBUG        
    	}	

        if (($s_USED == 1) && ($o_USED == 1)) {
		# -s greater than -o
            if ($start_parm>$stop_parm) {
		print "Stop Time (-o) must be greater than or equal to Start Time (-s)\n\n";
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
            }
	}

        if (($f_USED == 1) && ($c_USED == 1) && ($a_USED == 1)) {
		# Specified -f and -c and -a
		print "-f, -c and -a cannot all be used\n\n";
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}
	
	if (($f_USED != 1) && ($c_USED != 1) && ($a_USED != 1)) {
		#No valid flags used
		print "Either -f and -c or -a must be used\n\n";
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}
	
	if (($f_USED == 1) && ($c_USED != 1)) {
		# Specified -f, but not -c
		print "-f used, -c must also be used\n\n";
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}
	
	if (($f_USED != 1) && ($c_USED == 1)) {
		# Specified -c, but not -f
		print "-c used, -f must also be used\n\n";
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}

	if (($l_USED == 1) && ($r_USED != 1)) {
		#implicit call to -r
		$r_USED = 1;
	}

	if (($t_USED == 1) && ($r_USED != 1)) {
		#implicit call to -r
		$r_USED = 1;

	}

	if (($t_USED == 1) && ($l_USED != 1)) {
		print "Type is being changed and counters are being reset.\n";
        print "Lane is being reset to the default of 5.\n";
		print "If the default Lane is not wanted, change Type and Lane simultaneously.\n\n";
	}

	if (($l_USED == 1) && ($t_USED != 1)) {
		print "Lane is being changed and counters are being reset.\n";
        print "Type is being reset to the default of \"send\".\n";
		print "If the default Type is not wanted, change Type and Lane simultaneously.\n\n";
	}

	if (($r_USED == 1) && ($t_USED != 1) && ($l_USED != 1)) {
		print "The counters are being reset.\n";
        print "Lane is being reset to the default of 5.\n";
        print "Type is being reset to the default of \"send\".\n\n";
	}

	if ($f_USED == 1 || $a_USED == 1) {
		&validate_props;
	}
	
	if (($f_USED == 1) && ($c_USED == 1)) {
		$command = "i_stub_FS switchstats -C startswitch -f ";

		$command = $command . $frame_parm . " -c " . $cage_parm . " -p " . $lane_parm . " -y ";
 		$command = $command . $type_parm . " -a " . $start_parm . " -o " . $stop_parm . " -v ";
 		$command = $command . $interval_parm . " -m " . $filename_parm . " -z ";
        $command = $command . $filesize_parm . " -x "  . $r_USED . " -Y " . $directory_parm;

		chop($RC = `$BIN/$command`);

		$command2= "lsswstatactive -d $directory_parm";
		chop($RC2 = `./$command2`);
    }
	elsif ($a_USED == 1) {
		$command = "i_stub_FS switchstats -C startall -f ";

		$command = $command . $frame_parm . " -c " . $cage_parm . " -p " . $lane_parm . " -y ";
 		$command = $command . $type_parm . " -a " . $start_parm . " -o " . $stop_parm . " -v ";
 		$command = $command . $interval_parm . " -m " . $filename_parm . " -z ";
        $command = $command . $filesize_parm . " -x "  . $r_USED . " -Y " . $directory_parm;
                
		chop($RC = `$BIN/$command`);

		$command2= "lsswstatactive -d $directory_parm";
		chop($RC2 = `./$command2`);
		
	}
	else {
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}

    if ($RC==0)
    {
            print "Command successful.\n";
            print "lsswstatactive also executed.\n";
            print "Logs available at $directory_parm\n";
    }       
    if ($RC==50)
		{
			print "Cannot connect to FNM Daemon\n";
		}
		if ($RC==51)
		{
			print "One or more frame,cage parameters are invalid\n";
		}
		if ($RC==52)
		{
			print "Device Database not available\n";
		}
		if ($RC==70)
		{
			print "Unsupported Switch Statistics command.\n";
		}
		if ($RC==71)
		{
			print "Invalid frame and cage specified.\n";
		}
		if ($RC==72)
		{
			print "Counter already running on specified frame and cage.\n";
		}
		if ($RC==73)
		{
			print "Warning: Counter already running for one or more switches.\n";
            print "Command successful.\n";
		}
		if ($RC==74)
		{
			print "Counter for specified frame and cage is not running.\n";
		}
		if ($RC==75)
		{
			print "No switch counters running.\n";
		}
		if ($RC==76)
		{
			print "Thread failed to initialize.\n";
		}
		if ($RC==77)
		{
			print "Counter not running for one or more switches.\n";
            print "Command successful.\n";			
		}
        if ($RC==78)
        {
       	    print "Error opening directory specified.  Check to see if directory exists.\n";
        }	
        
$ret = 0;
$exit_routine;
}
else {
	print $USAGE;      #USAGE
	$ret = 1;
	&exit_routine;
}
